home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GP_UNIX.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  7KB  |  240 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gp_unix.c */
  20. /* Unix-specific routines for Ghostscript */
  21. #include "memory_.h"
  22. #include "string_.h"
  23. #include "gx.h"
  24. #include "gp.h"
  25. #include "stat_.h"
  26. #include "time_.h"
  27.  
  28. /* Library routines not declared in a standard header */
  29. extern char *getenv(P1(const char *));
  30.  
  31. /* popen isn't POSIX-standard, so we declare it here. */
  32. extern FILE *popen();
  33. extern int pclose();
  34.  
  35. /* Do platform-dependent initialization. */
  36. void
  37. gp_init(void)
  38. {
  39. }
  40.  
  41. /* Do platform-dependent cleanup. */
  42. void
  43. gp_exit(int exit_status, int code)
  44. {
  45. }
  46.  
  47. /* ------ Date and time ------ */
  48.  
  49. /* Read the current date (in days since Jan. 1, 1980) */
  50. /* and time (in milliseconds since midnight). */
  51. void
  52. gp_get_clock(long *pdt)
  53. {    long secs_since_1980;
  54.     struct timeval tp;
  55.     struct timezone tzp;
  56.     time_t tsec;
  57.     struct tm *tm, *localtime();
  58.  
  59.     if ( gettimeofday(&tp, &tzp) == -1 )
  60.        {    lprintf("Ghostscript: gettimeofday failed!\n");
  61.         gs_exit(1);
  62.        }
  63.  
  64.     /* tp.tv_sec is #secs since Jan 1, 1970 */
  65.  
  66.     /* subtract off number of seconds in 10 years */
  67.     /* leap seconds are not accounted for */
  68.     secs_since_1980 = tp.tv_sec - (long)(60 * 60 * 24 * 365.25 * 10);
  69.  
  70.     /* adjust for timezone */
  71.     secs_since_1980 -= (tzp.tz_minuteswest * 60);
  72.  
  73.     /* adjust for daylight savings time - assume dst offset is 1 hour */
  74.     tsec = tp.tv_sec;
  75.     tm = localtime(&tsec);
  76.     if ( tm->tm_isdst )
  77.         secs_since_1980 += (60 * 60);
  78.  
  79.     /* divide secs by #secs/day to get #days (integer division truncates) */
  80.     pdt[0] = secs_since_1980 / (60 * 60 * 24);
  81.     /* modulo + microsecs/1000 gives number of millisecs since midnight */
  82.     pdt[1] = (secs_since_1980 % (60 * 60 * 24)) * 1000 + tp.tv_usec / 1000;
  83. #ifdef DEBUG_CLOCK
  84.     printf("tp.tv_sec = %d  tp.tv_usec = %d  pdt[0] = %ld  pdt[1] = %ld\n",
  85.         tp.tv_sec, tp.tv_usec, pdt[0], pdt[1]);
  86. #endif
  87. }
  88.  
  89. /* ------ Printer accessing ------ */
  90.  
  91. /* Open a connection to a printer.  A null file name means use the */
  92. /* standard printer connected to the machine, if any. */
  93. /* "|command" opens an output pipe. */
  94. /* Return NULL if the connection could not be opened. */
  95. FILE *
  96. gp_open_printer(char *fname, int binary_mode)
  97. {    return
  98.       (strlen(fname) == 0 ?
  99.        gp_open_scratch_file(gp_scratch_file_name_prefix, fname, "w") :
  100.        fname[0] == '|' ?
  101.        popen(fname + 1, "w") :
  102.        fopen(fname, "w"));
  103. }
  104.  
  105. /* Close the connection to the printer. */
  106. void
  107. gp_close_printer(FILE *pfile, const char *fname)
  108. {    if ( fname[0] == '|' )
  109.         pclose(pfile);
  110.     else
  111.         fclose(pfile);
  112. }
  113.  
  114. /* ------ File names ------ */
  115.  
  116. /* Define the character used for separating file names in a list. */
  117. const char gp_file_name_list_separator = ':';
  118.  
  119. /* Define the default scratch file name prefix. */
  120. const char gp_scratch_file_name_prefix[] = "gs_";
  121.  
  122. /* Define the string to be concatenated with the file mode */
  123. /* for opening files without end-of-line conversion. */
  124. const char gp_fmode_binary_suffix[] = "";
  125. /* Define the file modes for binary reading or writing. */
  126. const char gp_fmode_rb[] = "r";
  127. const char gp_fmode_wb[] = "w";
  128.  
  129. /* Create and open a scratch file with a given name prefix. */
  130. /* Write the actual file name at fname. */
  131. FILE *
  132. gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
  133. {    char *temp;
  134.     if ( (temp = getenv("TEMP")) == NULL )
  135.         strcpy(fname, "/tmp/");
  136.     else
  137.         strcpy(fname, temp);
  138.     strcat(fname, prefix);
  139.     /* Prevent trailing X's in path from being converted by mktemp. */
  140.     if ( *fname != 0 && fname[strlen(fname) - 1] == 'X' )
  141.         strcat(fname, "-");
  142.     strcat(fname, "XXXXXX");
  143.     mktemp(fname);
  144.     return fopen(fname, mode);
  145. }
  146.  
  147. /* Answer whether a file name contains a directory/device specification, */
  148. /* i.e. is absolute (not directory- or device-relative). */
  149. int
  150. gp_file_name_is_absolute(const char *fname, uint len)
  151. {    /* A file name is absolute if it starts with a /. */
  152.     return ( len >= 1 && *fname == '/' );
  153. }
  154.  
  155. /* Answer the string to be used for combining a directory/device prefix */
  156. /* with a base file name.  The file name is known to not be absolute. */
  157. const char *
  158. gp_file_name_concat_string(const char *prefix, uint plen,
  159.                const char *fname, uint len)
  160. {    if ( plen > 0 && prefix[plen - 1] == '/' )
  161.         return "";
  162.     return "/";
  163. }
  164.  
  165. /* ------ File operations ------ */
  166.  
  167. /* If the file given by fname exists, fill in its status and return 1; */
  168. /* otherwise return 0. */
  169. int
  170. gp_file_status(const char *fname, file_status *pstatus)
  171. {    struct stat sbuf;
  172.     /* The RS/6000 prototype for stat doesn't include const, */
  173.     /* so we have to explicitly remove the const modifier. */
  174.     if ( stat((char *)fname, &sbuf) < 0 ) return 0;
  175.     pstatus->size_pages = stat_blocks(&sbuf);    /* st_blocks is */
  176.                     /* missing on some systems, */
  177.                     /* see stat_.h */
  178.     pstatus->size_bytes = sbuf.st_size;
  179.     pstatus->time_referenced = sbuf.st_mtime;
  180.     pstatus->time_created = sbuf.st_ctime;
  181.     return 1;
  182. }
  183.  
  184. /* ------ File enumeration ------ */
  185.  
  186. /****** THIS IS NOT SUPPORTED ON UNIX SYSTEMS. ******/
  187. /* Amazingly enough, there is no standard Unix library routine */
  188. /* for enumerating the files matching a pattern, */
  189. /* or even for enumerating (conveniently) the files in a directory. */
  190.  
  191. struct file_enum_s {
  192.     char *pattern;
  193.     int first_time;
  194.     const gs_memory_procs *mprocs;
  195. };
  196.  
  197. /* Initialize an enumeration.  NEEDS WORK ON HANDLING * ? \. */
  198. file_enum *
  199. gp_enumerate_files_init(const char *pat, uint patlen,
  200.   const gs_memory_procs *mprocs)
  201. {    file_enum *pfen = (file_enum *)(*mprocs->alloc)(1, sizeof(file_enum), "gp_enumerate_files");
  202.     char *pattern;
  203.     if ( pfen == 0 ) return 0;
  204.     pattern = (*mprocs->alloc)(patlen + 1, 1,
  205.                 "gp_enumerate_files(pattern)");
  206.     if ( pattern == 0 ) return 0;
  207.     memcpy(pattern, pat, patlen);
  208.     pattern[patlen] = 0;
  209.     pfen->pattern = pattern;
  210.     pfen->mprocs = mprocs;
  211.     pfen->first_time = 1;
  212.     return pfen;
  213. }
  214.  
  215. /* Enumerate the next file. */
  216. /* PUNT: JUST RETURN THE PATTERN. */
  217. uint
  218. gp_enumerate_files_next(file_enum *pfen, char *ptr, uint maxlen)
  219. {    if ( pfen->first_time )
  220.     {    char *pattern = pfen->pattern;
  221.         uint len = strlen(pattern);
  222.         pfen->first_time = 0;
  223.         if ( len > maxlen )
  224.             return maxlen + 1;
  225.         strcpy(ptr, pattern);
  226.         return len;
  227.     }
  228.     return -1;
  229. }
  230.  
  231. /* Clean up the file enumeration. */
  232. void
  233. gp_enumerate_files_close(file_enum *pfen)
  234. {    const gs_memory_procs *mprocs = pfen->mprocs;
  235.     (*mprocs->free)(pfen->pattern, strlen(pfen->pattern) + 1, 1,
  236.             "gp_enumerate_files_close(pattern)");
  237.     (*mprocs->free)((char *)pfen, 1, sizeof(file_enum),
  238.             "gp_enumerate_files_close");
  239. }
  240.